<?php
class Controller_Admin_Layout extends Controller_Template
{
    public $template = "admin";
    protected $layout = 'admin';
    protected $assets_css = 'assets/admin/css';
    protected $assets_js = 'assets/admin/js';
    protected $assets_image = 'assets/admin/images';

    public function  before() {
        parent::before();
        if ( ! Api::load('user')->is_logged('admin'))
        {
            Request::current()->redirect('admin/login');
        }
        else
        {
            $this->template->user = Api::load('user')->get_user_info();
        }

        $global_vars = array(
            'assets_css' =>  URL::site($this->assets_css, TRUE),
            'assets_js' => URL::site($this->assets_js, TRUE),
            'assets_image' => URL::site($this->assets_image, TRUE),
            );

        foreach($global_vars as $key => $value)
        {
            View::bind_global($key, $value);
        }
        
        $this->template->title = 'Backend';
        $this->template->sidebar = View::factory('admin/genaral/sidebar');
        $this->template->main = ''; //View::factory('admin/genaral/main');
        
        $this->template->scripts = array();
        $this->template->styles = array();
    }


    //action method here

    public function  after() {
        
        $styles = array(
            URL::site($this->assets_css.'/reset.css'),
            URL::site($this->assets_css.'/style.css'),
            URL::site($this->assets_css.'/invalid.css'),
            URL::site($this->assets_css.'/colorbox.css'),
        );
        $scripts = array(
            URL::site($this->assets_js.'/jquery-1.6.min.js?' . time()),
            URL::site($this->assets_js.'/jquery.colorbox-min.js?' . time()),
            //URL::site($this->assets_js.'/wysiwyg.js'),
            //URL::site($this->assets_js.'/jquery.datePicker.js'),
            URL::site($this->assets_js.'/jquery.date.js?' . time()),
            URL::site($this->assets_js.'/sidebar.js?' . time()),            
            URL::site($this->assets_js.'/simpla.jquery.configuration.js?' . time()),
        );
        
        $this->template->scripts = Arr::merge($scripts, $this->template->scripts);
        $this->template->styles = Arr::merge($styles, $this->template->styles);
        
        parent::after();
    }

    public function set_content($view)
    {
        $this->template->main = $view;
    }
	
    public function add_scripts($scripts)
    {
        if(!is_array($scripts))
            $scripts = array($scripts);
        $this->template->scripts = Arr::merge($this->template->scripts,$scripts);
    }

    public function add_styles($styles)
    {
        if(!is_array($styles))
            $styles = array($styles);
        $this->template->styles = Arr::merge($this->template->styles,$styles);
    }
}